/**
 * Copyright &copy; 2012-2014 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.schedul.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.schedul.entity.Schclass;
import com.thinkgem.jeesite.modules.schedul.service.SchclassService;

/**
 * 考勤时段表Controller
 * @author pan.zhou
 * @version 2017-07-15
 */
@Controller
@RequestMapping(value = "${adminPath}/schedul/schclass")
public class SchclassController extends BaseController {

	@Autowired
	private SchclassService schclassService;
	
	@ModelAttribute
	public Schclass get(@RequestParam(required=false) String id) {
		Schclass entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = schclassService.get(id);
		}
		if (entity == null){
			entity = new Schclass();
		}
		return entity;
	}
	
	@RequiresPermissions("schedul:schclass:view")
	@RequestMapping(value = {"list", ""})
	public String list(Schclass schclass, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<Schclass> page = schclassService.findPage(new Page<Schclass>(request, response), schclass); 
		model.addAttribute("page", page);
		return "modules/schedul/schclassList";
	}

	@RequiresPermissions("schedul:schclass:view")
	@RequestMapping(value = "form")
	public String form(Schclass schclass, Model model) {
		model.addAttribute("schclass", schclass);
		return "modules/schedul/schclassForm";
	}
    /**
     * 保存考勤时段
     * @param schclass
     * @param model
     * @param redirectAttributes
     * @return
     */
	@RequiresPermissions("schedul:schclass:edit")
	@RequestMapping(value = "save")
	public String save(Schclass schclass, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, schclass)){
			return form(schclass, model);
		}
		schclassService.save(schclass);
		addMessage(redirectAttributes, "保存考勤时段表成功");
		return "redirect:"+Global.getAdminPath()+"/schedul/schclass/list?repage";
	}
	
	@RequiresPermissions("schedul:schclass:edit")
	@RequestMapping(value = "delete")
	public String delete(Schclass schclass, RedirectAttributes redirectAttributes) {
		schclassService.delete(schclass);
		addMessage(redirectAttributes, "删除考勤时段表成功");
		return "redirect:"+Global.getAdminPath()+"/schedul/schclass/list?repage";
	}
	
	@RequiresPermissions("schedul:schclass:view")
    @RequestMapping(value = "getList")
	public String getList(Schclass schclass, HttpServletRequest request, HttpServletResponse response, Model model) {
        List<Schclass> schclassList = schclassService.findList(schclass); 
        model.addAttribute("schclassList", schclassList);
        return "modules/schedul/addSchedulingForm";
    }
	
	/**
	 *  异步调用，查询考勤时段列表
	 * @param Schclass
	 * @param response
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = {"getSchclassList"})
	public Map<String,Object> getSchclassList(Schclass schclass, HttpServletRequest request,HttpServletResponse response, Model model) {
		response.setContentType("application/json; charset=UTF-8");
		Map<String,Object> resultMap = new HashMap<String,Object>();
		try
		{
			Page<Schclass> page = schclassService.findPage(new Page<Schclass>(request, response), schclass); 
			if(null == page){
				resultMap.put("success", false);
			}else{
				resultMap.put("success", true);
				if(null != page.getList() && page.getList().size()>0)
				{
					resultMap.put("data", page);
					model.addAttribute("page", page);
				}			
			}
		} catch (Exception e)
		{
			resultMap.put("success", false);
			e.printStackTrace();
		}
		return resultMap;
	}
	
	/**
	 * 获取考勤时段JSON数据。
	 * @param 
	 * @param response
	 * @return
	 */
	@RequiresPermissions("schedul:schclass:view")
	@ResponseBody
	@RequestMapping(value = "treeData")
	public List<Map<String, Object>> treeData(HttpServletResponse response) {
		List<Map<String, Object>> mapList = Lists.newArrayList();
		List<Schclass> list = schclassService.findList(new Schclass());
		for (int i=0; i<list.size(); i++){
				Schclass e = list.get(i);
				Map<String, Object> map = Maps.newHashMap();
				map.put("id", e.getId());
				map.put("name", e.getSchname());
				
				mapList.add(map);
		}
		return mapList;
	}

}